/* *
 * --ライセンスについて--
 *
 * 「本ファイルの内容は Mozilla Public License Version 1.1 (「本ライセンス」)
 * の適用を受けます。
 * 本ライセンスに従わない限り本ファイルを使用することはできません。
 * 本ライセンスのコピーは http://www.mozilla.org/MPL/ から入手できます。
 *
 * 本ライセンスに基づき配布されるソフトウェアは、「現状のまま」で配布されるものであり、
 * 明示的か黙示的かを問わず、いかなる種類の保証も行われません。
 * 本ライセンス上の権利および制限を定める具体的な文言は、本ライセンスを参照してください。
 *
 * オリジナルコードおよび初期開発者は、N_H (h.10x64@gmail.com) です。
 *
 * N_H によって作成された部分の著作権表示は次のとおりです。
 *
 * Copyright (C)N_H 2012
 *
 * このファイルの内容は、上記に代えて、
 * GNU General License version2 以降 (以下 GPL とする)、
 * GNU Lesser General Public License Version 2.1 以降 (以下 LGPL とする)、
 * の条件に従って使用することも可能です。
 * この場合、このファイルの使用には上記の条項ではなく GPL または LGPL の条項が適用されます。
 * このファイルの他者による使用を GPL または LGPL の条件によってのみ許可し、
 * MPL による使用を許可したくない対象者は、上記の条項を削除することでその意思を示し、
 * 上記条項を GPL または LGPL で義務付けられている告知およびその他の条項に置き換えてください。
 * 対象者が上記の条項を削除しない場合、
 * 受領者は MPL または GPL または LGPL ライセンスのいずれによってもこのファイルを
 * 使用することができます。」
 *
 * -- License --
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License。You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND、either express or implied。See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *   N_H (h.10x64@gmail.com).
 *
 * Portions created by the Initial Developer are Copyright (C)N_H 2012
 * the Initial Developer。All Rights Reserved.
 *
 * Alternatively、the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL")、or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above。If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL、and not to allow others to
 * use your version of this file under the terms of the MPL、indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL。If you do not delete
 * the provisions above、a recipient may use your version of this file under
 * the terms of any one of the MPL、the GPL or the LGPL.
 *
 * */
package com.magiciansforest.audio.soundrenderer.logic;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Iterator;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 *
 * @author N_H
 */
public class MovieFile {

    private static final int PMD_PATH = 0;
    private static final int VMD_PATH = 1;

    public static boolean readMovieFile(File f, Movie mov) throws IOException, ScriptException {
        mov.reset();
        ScriptEngineManager scriptManager = new ScriptEngineManager();
        ScriptEngine javaScriptEngine = scriptManager.getEngineByName("JavaScript");
        javaScriptEngine.put("mov", mov);
        Reader r = new InputStreamReader(new FileInputStream(f), Charset.forName("UTF-8"));
        javaScriptEngine.eval(r);
        return true;
    }

    public static boolean writeMovieFile(File f, Movie mov) throws IOException {
        PrintWriter writer = new PrintWriter(f);

        writer.println("// Movie //");
        writer.println();
        writer.println("/* Class */");
        writer.println("var Camera = function(vmdFilePath) {");
        writer.println("    this.vmdFilePath = vmdFilePath;");
        writer.println("}");
        writer.println("var SoundSource = function(wavFilePath, name, boneName, startFrame, endFrame, volume, posX, posY, posZ) {");
        writer.println("    this.wavFilePath = wavFilePath;");
        writer.println("    this.name = name;");
        writer.println("    this.boneName = boneName;");
        writer.println("    this.startFrame = startFrame;");
        writer.println("    this.endFrame = endFrame;");
        writer.println("    this.volume = volume;");
        writer.println("    this.posX = posX;");
        writer.println("    this.posY = posY;");
        writer.println("    this.posZ = posZ;");
        writer.println("    this.newID = -1;");
        writer.println("}");
        writer.println("var Character = function(id, pmdPath, vmdPath, sources) {");
        writer.println("    this.id = id;");
        writer.println("    this.pmdPath = pmdPath;");
        writer.println("    this.vmdPath = vmdPath;");
        writer.println("    this.sources = sources;");
        writer.println("}");
        writer.println();
        writer.println("/* Data */");
        writer.print("camera = ");
        if (mov.getCameraVMDFilePath() != null) {
            writer.println("new Camera(\"" + mov.getCameraVMDFilePath().replace("\\", "\\\\") + "\");");
        } else {
            writer.println("0;");
        }
        Iterator<MMDSoundSource> srcItr = mov.getSourceIterator();
        writer.println("sourceIDs = Array();");
        writer.println("sourceList = Array();");
        while (srcItr.hasNext()) {
            MMDSoundSource src = srcItr.next();
            writer.println("sourceList[" + src.getID() + "] = new SoundSource(\n"
                    + "    \"" + src.getWavFilePath().replace("\\", "\\\\") + "\", \n"
                    + "    \"" + src.getName() + "\", \n"
                    + "    \"" + src.getBoneName() + "\", \n"
                    + "    " + src.getStartFrame() + ",\n"
                    + "    " + src.getEndFrame() + ",\n"
                    + "    " + src.getVolume() + ",\n"
                    + "    " + src.getSource().getLocalTranslation().x + ",\n"
                    + "    " + src.getSource().getLocalTranslation().y + ",\n"
                    + "    " + src.getSource().getLocalTranslation().z + "\n"
                    + ");");
            writer.println("sourceIDs.push(" + src.getID() + ");");
        }
        writer.println();
        Iterator<MMDCharacter> cItr = mov.getCharacterIterator();
        writer.println("characterList = Array();");
        int counter = 0;
        while (cItr.hasNext()) {
            MMDCharacter c = cItr.next();
            writer.println("characterList[" + counter + "] = new Character(\n"
                    + "    " + c.getID() + ", \n"
                    + "    \"" + c.getPMDFilePath().replace("\\", "\\\\") + "\", \n"
                    + "    \"" + c.getVMDFilePath().replace("\\", "\\\\") + "\", \n"
                    + "    " + "[");
            for (int i = 0; i < c.getSourceCount(); i++) {
                writer.println("        sourceList[" + c.getSoundSources().get(i).getID() + ((i < c.getSourceCount() - 1) ? "]," : "]"));
            }
            writer.println("    ]);");
            counter++;
        }
        writer.println();
        writer.println("/* Method */");
        writer.println("if (camera != 0) {");
        writer.println("    mov.bindCameraVMD(mov.loadVMDFile(camera.vmdFilePath));");
        writer.println("}");
        writer.println();
        writer.println("for (i = 0; i < sourceIDs.length; i++) {");
        writer.println("    sourceList[sourceIDs[i]].newID = mov.addSoundSource();");
        writer.println("    src = sourceList[sourceIDs[i]];");
        writer.println("    mov.setSoundSourceName(src.newID, src.name);");
        writer.println("    mov.setWavFile(src.newID, src.wavFilePath);");
        writer.println("    mov.setSoundSourceStartTime(src.newID, src.startFrame);");
        writer.println("    mov.setSoundSourceEndTime(src.newID, src.endFrame);");
        writer.println("    mov.setSoundSourceVolume(src.newID, src.volume);");
        writer.println("    mov.setTranslation(src.newID, src.posX, src.posY, src.posZ);");
        writer.println("}");
        writer.println();
        writer.println("for (i = 0; i < characterList.length; i++) {");
        writer.println("    c = characterList[i];");
        writer.println("    id = mov.loadPMDFile(c.pmdPath);");
        writer.println("    mov.bind(id, mov.loadVMDFile(c.vmdPath));");
        writer.println("    for (j = 0; j < characterList[i].sources.length; j++) {");
        writer.println("        src = characterList[i].sources[j];");
        writer.println("        mov.bind(src.newID, id, src.boneName);");
        writer.println("    }");
        writer.println("}");

        writer.flush();
        writer.close();
        return true;
    }
}
